package com.googlecode.spruce.web.admin.os;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.googlecode.spruce.dao.OperatingSystemDao;
import com.googlecode.spruce.domain.OperatingSystem;

@Controller
@RequestMapping("/admin/os")
public class OperatingSystemDashboard
{

    @Autowired
    private OperatingSystemDao operatingSystemDao;

    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    public ModelAndView getAll()
    {
        final ModelAndView model = new ModelAndView("os/index");

        final List<OperatingSystem> osList = this.operatingSystemDao.getAll();

        model.addObject("osList", osList);

        return model;
    }

    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public ModelAndView add(@RequestParam("vendor") String vendor, @RequestParam("product") String product,
            @RequestParam("version") String version)
    {
        final OperatingSystem os = new OperatingSystem();

        os.setVendor(vendor);
        os.setProduct(product);
        os.setVersion(version);

        this.operatingSystemDao.save(os);

        return getAll();
    }
}
